package com.zby.lease.web.admin.controller.apartment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.lease.common.result.Result;
import com.zby.lease.model.enums.ReleaseStatus;
import com.zby.lease.model.pojo.RoomInfo;
import com.zby.lease.web.admin.service.RoomInfoService;
import com.zby.lease.web.admin.service.imp.RoomInfoServiceImpl;
import com.zby.lease.web.admin.vo.room.RoomDetailVo;
import com.zby.lease.web.admin.vo.room.RoomItemVo;
import com.zby.lease.web.admin.vo.room.RoomQueryVo;
import com.zby.lease.web.admin.vo.room.RoomSubmitVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
@CrossOrigin
public class RoomController {
    @Autowired
    private RoomInfoService roomService;
    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
   roomService.saveOrUpdateRoom(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        IPage<RoomItemVo> page = new Page<>(current, size);
      IPage<RoomItemVo> pageByQuery =  roomService.pageRoomItemByQuery(page,queryVo);
        return Result.ok(pageByQuery);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {

       RoomDetailVo roomDetailVo  = roomService.getDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        // 创建LambdaUpdateWrapper对象，用于后续更新操作
        LambdaUpdateWrapper<RoomInfo> updateWrapper = new LambdaUpdateWrapper<>();

        // 设置更新条件，根据RoomInfo类的id字段值与传入的id参数进行比较
        updateWrapper.eq(RoomInfo::getId, id);

        // 设置更新内容，将RoomInfo类的isRelease字段值更新为传入的status参数
        updateWrapper.set(RoomInfo::getIsRelease, status);

        // 调用roomService的update方法执行更新操作
        roomService.update(updateWrapper);
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        // 创建LambdaQueryWrapper对象，用于构建查询条件
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件为房间所属公寓ID等于给定的ID
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        // 设置查询条件为房间发布状态为已发布
        queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
        // 调用roomService的list方法，根据查询条件获取房间信息列表
        List<RoomInfo> roomInfoList = roomService.list(queryWrapper);
        // 返回包含房间信息列表的Result对象
        return Result.ok(roomInfoList);

    }

}


















